package it.blueocean.acanto.taglibrary.jsp.taglib.tags.container;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractComponentStateBean;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractUIComponentTag;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.ComponentStateBeanNotFoundException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

public class ContainerTag extends AbstractUIComponentTag
{
	private Map savedAttributes;

	public AbstractComponentStateBean initializeState() throws JspException
	{
		return new ContainerStateBean();
	}

	public int doStartTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		this.savedAttributes = new HashMap();
		return 1;
	}

	public AbstractComponentStateBean getComponent(String paramString) throws ComponentStateBeanNotFoundException
	{
		return ((ContainerStateBean) getStateBean()).getComponent(paramString);
	}

	public void addComponent(AbstractComponentStateBean paramAbstractComponentStateBean)
	{
		Object localObject = this.pageContext.getAttribute(paramAbstractComponentStateBean.getId());
		this.savedAttributes.put(paramAbstractComponentStateBean.getId(), localObject);
		this.pageContext.setAttribute(paramAbstractComponentStateBean.getId(), paramAbstractComponentStateBean);
	}

	public int doEndTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		Iterator localIterator = this.savedAttributes.entrySet().iterator();
		while (localIterator.hasNext())
		{
			Map.Entry localEntry = (Map.Entry) localIterator.next();
			this.pageContext.removeAttribute((String) localEntry.getKey());
			if(localEntry.getValue() != null)
				this.pageContext.setAttribute((String) localEntry.getKey(), localEntry.getValue());
		}
		return 6;
	}

}
